package com.javaex.book.service.impl;

import java.util.List;

import com.javaex.book.dao.BookDao;
import com.javaex.book.dao.OrderDao;
import com.javaex.book.dao.OrderItemDao;
import com.javaex.book.dao.impl.BookDaoImpl;
import com.javaex.book.dao.impl.OrderDaoImpl;
import com.javaex.book.dao.impl.OrderItemDaoImpl;
import com.javaex.book.pojo.Book;
import com.javaex.book.pojo.Cart;
import com.javaex.book.pojo.CartItem;
import com.javaex.book.pojo.Order;
import com.javaex.book.pojo.OrderItem;
import com.javaex.book.service.OrderService;
import com.javaex.book.utils.Constants;
import com.javaex.book.utils.ReturnResult;

public class OrderServiceImpl implements OrderService {

	private OrderDao orderDao = new OrderDaoImpl();
	private OrderItemDao orderItemDao = new OrderItemDaoImpl();
	private BookDao bookDao = new BookDaoImpl();
	
	@Override
	public String createOrder(Cart cart, Integer userId) {
		String orderId = System.currentTimeMillis() + "" + userId;
		// 保存订单
		Order order = new Order(orderId, userId, cart.getTotalPrice());
		orderDao.saveOrder(order);	
		for (CartItem item : cart.getItems().values()) {
			// 保存订单项
			OrderItem orderItem = new OrderItem(item.getName(), item.getCount(), item.getPrice(), orderId, item.getTotalPrice());
			orderItemDao.saveOrderItem(orderItem);
			
			// 修改商品的库存和销量
			Book book = bookDao.queryBookById(item.getId());
			book.setSales(book.getSales() + item.getCount());
			book.setStock(book.getStock() - item.getCount());
			bookDao.updateBook(book);
		}
		return orderId;
	}
	
	@Override
	public Integer countMyOrders(Integer userId) {
		return orderDao.countOrders(userId);
	}
	
	@Override
	public List<Order> showMyOrders(Integer userId, Integer start, Integer pageSize) {
		return orderDao.queryOrdersByUserId(userId, start, pageSize);
	}
	
	@Override
	public Integer countAllOrders() {
		return orderDao.countOrders(null);
	}
	
	@Override
	public List<Order> showAllOrders(Integer start, Integer pageSize) {
		return orderDao.queryOrders(start, pageSize);
	}
	
	@Override
	public ReturnResult receiverOrder(String orderId) {
		ReturnResult result = new ReturnResult();
		Order order = orderDao.queryOrderById(orderId);
		if (order == null) {
			return result.fail("订单 " + orderId + " 不存在");
		}
		if (order.getStatus() == Constants.ORDER_NOT_SEND) {
			return result.fail("订单 " + orderId + " 还未发货哦");
		}
		if (order.getStatus() == Constants.ORDER_RECEIVED) {
			return result.fail("订单 " + orderId + " 已经签收了");
		}
		// 签收订单（改变 status 状态）
		Integer rows = orderDao.changeOrderStatus(orderId, Constants.ORDER_RECEIVED);
		if (rows == 0) {
			return result.fail("订单 " + orderId + " 签收失败");
		}
		return result.success();
	}
	
	@Override
	public Integer countOrderItems(String orderId) {
		return orderItemDao.countOrderItems(orderId);
	}

	@Override
	public List<OrderItem> showOrderDetail(String orderId, Integer start, Integer pageSize) {
		return orderItemDao.queryOrderItemsByOrderId(orderId, start, pageSize);
	}
	
	@Override
	public ReturnResult deleteOrder(String orderId) {
		ReturnResult result = new ReturnResult();
		// 查询订单是否已签收，未发货、未签收的订单不能删除
		Order order = orderDao.queryOrderById(orderId);
		if (order.getStatus() != Constants.ORDER_RECEIVED) {
			return result.fail("订单 " + orderId + " 还未签收，不能删除");
		}
		
		// 删除所有订单项
		Integer rows = orderItemDao.deleteOrderItems(orderId);
		if (rows == 0) {
			return result.fail("订单 " + orderId + " 删除失败");
		}
		// 删除订单
		rows = orderDao.deleteOrder(orderId);
		if (rows == 0) {
			return result.fail("订单 " + orderId + " 删除失败");
		}
		return result.success();
	}

	@Override
	public ReturnResult sendOrder(String orderId) {
		ReturnResult result = new ReturnResult();
		// 根据订单id查询出订单
		Order order = orderDao.queryOrderById(orderId);
		
		// 判断订单是否未发货，如果已发货或已签收，返回提示信息
		if (order.getStatus() == Constants.ORDER_SEND) {
			return result.fail("订单 " + orderId + " 已经发货了");
		}
		if (order.getStatus() == Constants.ORDER_RECEIVED) {
			return result.fail("订单 " + orderId + " 已经被签收了");
		}
		
		// 如果未发货，将订单的状态改为已发货
		Integer rows = orderDao.changeOrderStatus(orderId, Constants.ORDER_SEND);
		if (rows == 0) {
			return result.fail("订单 " + orderId + " 发货失败");
		}
		return result.success();
	}

}
